var iFDU = require('../../utils/iFDU.js');
var app = getApp();
Page({
  data: {
    config: app.config,
    globalData: app.globalData,

    addresses: [],
    needCb: 0,
    first: true,
  },

  onLoad: function (options) {
    if (options.needCb) {
      this.setData({
        needCb: options.needCb
      });
    }
  },

  loadAddressList: function () {
    var that = this;
    wx.showLoading({
      title: '加载中',
    });
    app.getUserInfo(function (globalData) {
      that.setData({
        globalData: globalData
      });
      
      iFDU.api.doGet(
        'address/getAddressList',
        {
          userId: globalData.userId,
          rnd: Math.random()
        },
        function (data) {
          if (that.data.first) {
            if (!data || data.length < 1) {
              that.addNewAddress();
              that.setData({
                first: false,
              });
            } else {
              that.setData({
                first: false,
                addresses: data
              });
            }
          } else {
            if (!data || data.length < 1) {
              that.setData({
                addresses: data
              });
            } else {
              that.setData({
                addresses: data
              });
            }
          }
          
          wx.hideLoading();
        }
      );
    });
    
  },

  onShow: function () {
    this.loadAddressList();
  },

  setDefaultAddress: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    iFDU.api.doGet(
      'address/setDefaultAddress',
      {
        userId: that.data.globalData.userId,
        id: id
      },
      function (data) {
        var addresses = that.data.addresses;
        var len = addresses.length;
        for (var i = 0; i < len; i++) {
          if (id == addresses[i].id) {
            addresses[i].is_default = 1;
          } else {
            addresses[i].is_default = 0;
          }
        }

        that.setData({
          addresses: addresses
        });
      }
    )
  },

  editAddress: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../address-new/address-new?addressId=' + id,
    })
  },

  addNewAddress: function () {
    var that = this;
    if (!that.data.needCb || that.data.needCb == 0) {
      wx.navigateTo({
        url: '../address-new/address-new?needCb=' + that.data.needCb,
      })
    } else {
      wx.redirectTo({
        url: '../address-new/address-new?needCb=' + that.data.needCb,
      })
    }
  },

  deleteAddress: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '删除收货地址',
      content: '将此组数据从列表中删除吗？',
      confirmText: "删除",
      cancelText: "保留",
      success: function (res) {
        console.log(res);
        if (res.confirm) {
          iFDU.api.doGet(
            'address/deleteAddress',
            {
              userId: that.data.globalData.userId,
              id: id
            },
            function (data) {
              console.log('load address list')
              that.loadAddressList();
            }
          )
        }
      }
    });
  },

  selectAddress: function (e) {
    var that = this;
    if (!that.data.needCb || that.data.needCb == 0) {
      return;
    }
    var id = e.currentTarget.dataset.id;
    iFDU.api.doGet(
      'address/getAddressById',
      {
        userId: that.data.globalData.userId,
        id: id,
        rnd: Math.random()
      },
      function (data) {
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];
        prevPage.setData({
          addressId: id,
          address: data.address
        })

        wx.navigateBack({
        })
      }
    )
  },

  onShareAppMessage: function (res) {
    var that = this;
    return {
      title: '爱上衬衫，从「逸见」开始…',
      imageUrl: 'http://s.i-fdu.com/runto/wxa/s/yjshare.jpeg',
      path: 'pages/home/home',
      success: function (res) {
      },
      fail: function (res) {
      }
    }
  }
})